package login;



import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import platform.AppConfig;
import protocol.ParameterType;
import protocol.ReturnCode;
import io.ClientSession;
import io.OperationResponse;


public class PPlogin {
	protected static final Logger log = LoggerFactory.getLogger(PPlogin.class);

	static String Appid = "4641";
	static String ServiceUrl = "http://passport_i.25pp.com:8080/account?tunnel-command=2852126760";
	static String Appkey="44fed0bd85500a3b97d3e10b74dc50e4";
	public static void Login(OperationResponse operationRequest,
			ClientSession session ) {
		try {
		 if (operationRequest.Parameters.size() < 2) {
		 OperationResponse response = new OperationResponse(
		 operationRequest.OperationCode, null, ReturnCode.No, "");
		 session.SendOperationResponse(response);
		 }
		 // String id="2454026_5519vok1mmgl8j0d03h6l0idj6";
		 String id = (String) operationRequest.Parameters
		 .get(ParameterType.UserID);
		 String token = (String) operationRequest.Parameters
		 .get(ParameterType.UserPwd);
		 String deviceID = (String) operationRequest.Parameters
		 .get(ParameterType.DeviceID);
		 String isGetIP = (String) operationRequest.Parameters
		 .get(ParameterType.IsGetIP);
		 String PhoneType=(String) operationRequest.Parameters
		 .get(ParameterType.PhoneType);
		
		 if (id.equals("zealm")&&token.equals("")) {
		 OperationResponse response = new OperationResponse(
		 operationRequest.OperationCode, AppConfig.dicAppInfo,
		 ReturnCode.GetServer, "");
		 session.SendOperationResponse(response);
		 return;
		
		 }
		//
		String time = Long.toString(System.currentTimeMillis());
		String sign =  Util.getMD5Str(id + Appkey);
		 HashMap<String,Object> obj = new  HashMap<String,Object>();
		 HashMap<String,Object> obj1 = new  HashMap<String,Object>();
		 HashMap<String,Object> obj2 = new  HashMap<String,Object>();
		 obj2.put("gameId", Appid);
		 obj1.put("sid", id);
		 String date=hashMapToJson(obj1);
		 String game=hashMapToJson(obj2);
		
		 obj.put("id", time);
         obj.put("service", "account.verifySession");
         obj.put("data", date);
         obj.put("game", game);
         obj.put("encrypt", "md5");
         obj.put("sign", sign);
         
		String body=hashMapToJson(obj);
		
		
		
			body = new String(body.getBytes("gbk"), "utf-8");
			
			String result =Util.sendPost(ServiceUrl, body);
			if (!result.equals("")) {
				
			
			JSONObject state=new JSONObject(result);
			JSONObject data=state.getJSONObject("state");
			String code=data.optString("code");
			if(code.equals("1")){
			data=state.getJSONObject("data");
			id=data.optString("accountId");
			operationRequest.Parameters.put(ParameterType.UserID, id);
			HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
			myPrams.put(ParameterType.UserID, id);
			OperationResponse responseID = new OperationResponse(
					operationRequest.OperationCode, myPrams, ReturnCode.HasID,
					"");
			session.SendOperationResponse(responseID);
			Util.sdkReceived(operationRequest, session, id, deviceID, isGetIP,"PP",PhoneType,"");	
			}else {
				return;
			}
			}else {
				log.info("pp return have a error result ="+result);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	
	}
	
	
	  public static String hashMapToJson(HashMap map) {  
	        String string = "{";  
	        for (Iterator it = map.entrySet().iterator(); it.hasNext();) {  
	            Entry e = (Entry) it.next();
	            
	            String value = (String)e.getValue();
	            char[] temp = value.toCharArray();
	            if(temp[0]=='{')
	            {
	            	string +=  "\"" + e.getKey() + "\""+":";  
		            string +=  e.getValue() +","; 
	            }else {
	            	string +=  "\"" + e.getKey() + "\""+":";  
		            string +=  "\"" + e.getValue() + "\""+",";
				}     
	        }  
	        string = string.substring(0, string.lastIndexOf(","));  
	        string += "}";  
	        return string;  
	       
	    }  
	
	
	
	  public  Runnable getLoginPP(OperationResponse operationResponse, ClientSession session){ 
			class LoginPP implements Runnable { 
				private OperationResponse t_operationResponse;
				private ClientSession t_session;
				public LoginPP(OperationResponse operationResponse, ClientSession session)
				{
					t_operationResponse = operationResponse;
					t_session = session;
				}
				
				@Override
				public void run() {
						try {
							Login(t_operationResponse, t_session);
						} catch (Exception e) {
							log.info(Thread.currentThread().getName() + " ppLogin task Error!!!");
						}
					}
	        }
		    return new LoginPP(operationResponse, session); 
		 } 
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
